package com.hospital.pet.love.lovepethospital.config;

import com.hospital.pet.love.lovepethospital.interceptor.AuthenticationInterceptor;
import com.hospital.pet.love.lovepethospital.properties.AuthenticationProperties;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author RoselleShaw
 * @create 2024-04-19 11:16
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private AuthenticationInterceptor authenticationInterceptor;
    @Resource
    private AuthenticationProperties authenticationProperties;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")      // 添加路径规则
                .allowCredentials(true)               // 是否允许在跨域的情况下传递Cookie
                .allowedOriginPatterns("*")           // 允许请求来源的域规则
                .allowedMethods("*")
                .allowedHeaders("*");                // 允许所有的请求头
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(authenticationProperties.getNoAuth())
                // 接口文档路径
                .excludePathPatterns("/static/**", "/webjars/**", "/doc.html", "/v3/**");
    }
}
